package com.duan.laugh.sms.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.duan.laugh.sms.api.constans.SmsConstants.*;


/**
 * 短信队列
 *
 * @author duanjw
 */
@Configuration
@Slf4j
public class QueueConfig {

    /**
     * 短信队列
     * @return
     */
    @Bean("smsQueue")
    public Queue smsQueue() {
        return new Queue(MQ_QUEUE_SMS);
    }

    /**
     * 短信延迟队列
     * @return
     */
    @Bean("smsDelayQueue")
    public Queue smsDelayQueue() {
        return new Queue(MQ_QUEUE_SMS_DELAY);
    }

    /**
     * 配置默认的交换机
     *
     * @return
     */
    @Bean
    CustomExchange customExchange() {
        Map<String, Object> args = new HashMap<>(1);
        args.put("x-delayed-type", "direct");
        //参数二为类型：必须是x-delayed-message
        return new CustomExchange(MQ_EXCHANGE_SMS_DELAY, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到交换器
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    Binding binding(@Qualifier("smsQueue")Queue queue, CustomExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(MQ_QUEUE_SMS_DELAY).noargs();
    }
}
